package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HCar;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HVisitor;
import com.flea.hclv.service.HCarService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: project
 * @description: 车辆
 * @author: shiliqiang
 * @create: 2018-09-26 20:41
 * @version: 1.0
 */
@RestController
@RequestMapping("/flea/hclv/car/")
public class HCarController {

    @Autowired
    private HCarService hCarService;

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public ServerResponse<String> saveOrUpdate(HttpServletRequest request){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        String model = request.getParameter("model");
        String image = JSON.parseObject(model).getString("images");
        List<HResourceRelation> hResourceRelations = JSON.parseArray(image,HResourceRelation.class);
        HCar hCar = JSON.parseObject(model,HCar.class);
        return hCarService.saveOrUpdate(userId,currentUserDept,hCar,hResourceRelations);
    }

    /**
     * 查询所有车辆  （供管理员使用）
     * @param request request
     * @param paging 分页对象
     * @param name 车辆所有人的姓名
     * @param type 车辆的类型
     * @return list
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public ServerResponse<FleaPage<HCar>> selectAll(HttpServletRequest request,FleaPage<HVisitor> paging,
                                    @RequestParam(name = "name",defaultValue = "",required = false)String name,
                                    @RequestParam(name = "type",defaultValue = "",required = false)String type){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hCarService.selectAll(currentUserDept,paging.getCurPage(),paging.getPageSize(),name,type);
    }

    /**
     * 按部门查询车辆  （部门指：供应商，地接社等）
     * @param request request
     * @param paging 分页对象
     * @param name 车辆所有人的姓名
     * @param type
     * @return list
     */
    @RequestMapping("selectByDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<HCar>> selectByDeptId(HttpServletRequest request,FleaPage<HVisitor> paging,
                                                    @RequestParam(name = "name",defaultValue = "",required = false)String name,
                                                    @RequestParam(name = "type",defaultValue = "",required = false)String type){
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        return hCarService.selectByDeptId(currentUserDept,paging.getCurPage(),paging.getPageSize(),name,type);
    }

    @RequestMapping("deleteByIds")
    @ResponseBody
    public ServerResponse<String> deleteByIds(HttpServletRequest request, @RequestParam("ids")String ids){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<String> idList = JSON.parseArray(ids,String.class);
        return hCarService.deleteByIds(userId,idList);
    }

    @RequestMapping("selectImageById")
    @ResponseBody
    public ServerResponse<List<HResourceRelation>> selectImageById(String carId){
        return  hCarService.selectImageById(carId);
    }
}
